﻿namespace Home
{
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Home.Repository;
    using Newtonsoft.Json.Linq;

    public class WebsiteCommentCommand : WebsiteCommand
    {
        protected override string EntitySetName
        {
            get { return "Comments"; }
        }

        protected override string OptimizationCategory
        {
            get { return "Comment"; }
        }

        protected override WebsiteSynchronizationType SynchronizationType
        {
            get { return WebsiteSynchronizationType.OptimizationComment; }
        }

        protected override void AddEntity(ServiceDbContext database, WebsiteEntity entity)
        {
            database.Comments.Add((OptimizationComment)entity);
        }

        protected override void FulfillEntity(JToken json, WebsiteEntity entity)
        {
            var obj = (OptimizationComment)entity;
            obj.VisitorId = (Guid)json["VisitorId"];
            obj.RegisteredUserId = (Guid?)json["RegisteredUserId"];
            obj.CategoryKey = (string)json["CategoryKey"];
            obj.CategoryValue = (string)json["CategoryValue"];
            obj.ParentId = (Guid?)json["ParentId"];
            obj.Content = (string)json["Content"];
            obj.Score = (int?)json["Score"];
        }

        protected override bool TryGetEntity(ServiceDbContext database, Guid websiteId, out WebsiteEntity entity)
        {
            entity = database.Comments.FirstOrDefault(e => e.WebsiteId == websiteId);

            if (entity == null)
            {
                entity = new OptimizationComment();
                return false;
            }

            return true;
        }
    }
}
